export const throttle = function (func: Function, delay: number) {
  let timer: any;
  let flgt = false;

  return function (...args: any) {
    if (!flgt) {
      flgt = true;
      // @ts-ignore
      func.apply(this, args);
      timer = setTimeout(() => {
        flgt = false;
      }, delay);
    }
  };
};

// 选择对象中存在的key值
export function pickObjKey(
  obj1: Record<string, unknown>,
  obj2: Record<string, unknown>
) {
  const tempObj = { ...obj1 };
  const commonKeys: Record<string, unknown> = {};

  for (let key in obj2) {
    if (tempObj.hasOwnProperty(key)) {
      commonKeys[key] = obj2[key] || tempObj[key];
    }
  }
  Object.assign(tempObj, commonKeys);
  return { ...tempObj };
}

export function getImg(url: string) {
  return new URL(`/src/assets/images/${url}`, import.meta.url).href;
}
